<?php
    /**
     * Created by PhpStorm.
     * 版权所有: 2019~2022 [ hhygyl ]
     * Date: 2019/8/28-11:13
     * Link: http://luckyadmin.luckyhhy.cn
     * FileName: Wxpay.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */


    namespace app\admin\controller;


    class Wxpay extends SystemBase
    {


        protected $options;
        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub

            $this->options = [
                'body'             => '测试商品',
                'out_trade_no'     => time(),
                'total_fee'        => '1',
                'openid'           => 'o38gpszoJoC9oJYz3UHHf6bEp0Lo',
                'trade_type'       => 'JSAPI',
                'notify_url'       => 'http://paysdk.weixin.qq.com/notify.php',
                'spbill_create_ip' => '127.0.0.1',
            ];
        }



        /**
         * @return mixed
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: index
         * @describe:JsApi及H5支付
         */
        public function index(){

            $wechat=new \WeChat\Pay($this->wx_config);
            // 组装参数，可以参考官方商户文档
            try {
                // 生成预支付码
                $result = $wechat->createOrder($this->options);

                // 创建JSAPI参数签名
                $options = $wechat->createParamsForJsApi($result['prepay_id']);

                var_dump($options);

            } catch (Exception $e) {

                $this->error($e->getMessage());
            }

            $this->assign("options",$options);
            return $this->fetch();

        }



        /**
         * @return mixed
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: native
         * @describe:扫码支付
         */
        public function native(){
            $wechat=new \WeChat\Pay($this->wx_config);
            // 组装参数，可以参考官方商户文档
            try {
                $this->options['trade_type']="NATIVE"; //更改支付模式
                // 生成预支付码
                $result = $wechat->createOrder($this->options);

                //
                $options = $wechat->createParamsForRuleQrc($result['product_id']);

                var_dump($options);

            } catch (Exception $e) {

                $this->error($e->getMessage());
            }

            $this->assign("url",$options);
            return $this->fetch();

        }









    }